//
// Created by 11510 on 2025/10/2.
//

#include "UnliveCommand.h"
#include "RegistryContainer.hpp"
#include "CameraApp.h"
xclib_client::UnliveCommand::UnliveCommand(CommandContext ctx) : ICommand("unlive", ctx) {};

std::optional<std::future<int>> xclib_client::UnliveCommand::run(const std::vector<std::string> &args, bool wait) {
    cxxopts::Options o("unlive", "unlive");
    o.add_options()("u,unit", "unit", cxxopts::value<uint16_t>()->default_value(""))("help", "help");
    try {
        auto r = parseOptions(o, args);
        if (r.count("help")) {
            std::cout << o.help() << "\n";
            return std::nullopt;
        }
        auto u = r["unit"].as<uint16_t>();
        auto task = [u]() {
            std::cout << "Stop live on '" << u << "'...\n";
            registry::RegistryContainer::instance().get<CameraApp>("lazy")->unlive(u);
            return 0;
        };
        if (!wait) return make_optional(std::async(std::launch::async, task));
        else {
            task();
            return std::nullopt;
        }
    } catch (const cxxopts::exceptions::exception &e) {
        std::cerr << "unlive: " << e.what() << "\n";
        return std::nullopt;
    }
}

std::string xclib_client::UnliveCommand::shortHelp() const { return "unlive -u <unitmap>"; }

std::string xclib_client::UnliveCommand::longHelp() const { return "unlive -u <unitmap>\n    stop live"; }
